home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 111_01 / labels.c < prev    next >
Text File  |  1985-08-19  |  5KB  |  252 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:        Labels;
  4. VERSION:    1.2;
  5.  
  6. DESCRIPTION:    "Prints labels from an address file.
  7.         File CUG.ADR is a sample address file.";
  8.  
  9. KEYWORDS:    Labels, address, mail;
  10. SYSTEM:        CP/M-80;
  11. FILENAME:    LABELS.C;
  12. WARNINGS:    "Documentation is cryptic.";
  13. SEE-ALSO:    CUG.ADR;
  14. AUTHORS:    Unknown;
  15. COMPILERS:    BDS C;
  16. */
  17. /************************************************************************
  18.  
  19.     Version 1.2:
  20.         Upgrade to BDS "C" v1.44
  21.  
  22.     Command syntax:
  23.  
  24.     A>LABELS <address file> outfile_name [-switches]
  25.  
  26.     Addresses may be selected individually, or by catagory.
  27.  
  28.     Switches:
  29.     
  30.     -p<key>    Select a Particular Address
  31.     -c<cat>    Select a catagory for mass labels
  32.  
  33.     Address file syntax: (each entry)
  34.  
  35.     comments ...
  36.     [individual_key] category] ... [phone]
  37.     Any number of address lines <two spaces><zipcode>
  38.     [greeting line of letter
  39.  
  40. ************************************************************************/
  41.  
  42. #include <bdscio.h>
  43.  
  44. #define MAXSTR 10        /* Maximum string length for matching */
  45. #define NFORMS 4        /* Number of Labels per page */
  46. #define FORMLINES 7        /* Number of lines that will fit on a label */
  47. #define FORMSPACE 3        /* Number of lines to skip between labels */
  48.  
  49. #define CR 0x0D
  50. #define LF 0x0A
  51. #define FF 0x0C
  52.  
  53. /* Global Data */
  54.  
  55. char
  56.     labcnt, lablin,
  57.     ans[5], stype, srchlen, i, chr, done,
  58.     srchstr[MAXSTR+2], scanbuf[MAXSTR+2];
  59. int
  60.     nlabls, ichr;
  61.  
  62. struct biobuf {bbf[BUFSIZ];};
  63.  
  64. struct biobuf adrs_file, labl_file;
  65. struct biobuf *adrs_fd, *labl_fd;
  66.  
  67. main(argc,argv)
  68. char argc, *argv[];
  69. {
  70.  printf("LABELS - Mailing Label Processor, v1.2\n");
  71.  
  72.  /* Attempt to open address file */
  73.  if ((adrs_fd = fopen(argv[1], &adrs_file)) == ERROR) {
  74.     printf("Cannot Open: %s", argv[1]); exit(); }
  75.  
  76.  /* Make the output file */
  77.  if ((labl_fd = fcreat(argv[2], &labl_file)) == ERROR) {
  78.     printf("Cannot Create: %s", argv[2]); exit(); }
  79.  
  80.  if (argc <= 3) {
  81.  
  82.  /* Conversation with the human */
  83.  
  84.     again:
  85.      printf("\nDo you want a particular address\n or a class of addresses");
  86.      printf(" [P or C]? ");
  87.      gets(ans);
  88.      if ((ans[0] = toupper(ans[0])) == 'P') stype = 0;    /* Key on '[' */
  89.      else if (ans[0] == 'C') stype = 1;    /* Key on blank following '[' */
  90.      else goto again;
  91.  
  92.      if (stype)
  93.       printf("\nEnter the class name: ");
  94.      else
  95.       printf("\nEnter the desired address key: ");
  96.     gets(srchstr);
  97.     }
  98.  
  99.     for (i = 3; argc-- > 3; ++i) {    /* Process Switches */
  100.     /* delete a leading '-' */
  101.     if (*argv[i] == '-') ++argv[i];
  102.  
  103.     switch (tolower(*argv[i])) {
  104.  
  105.         case 'p':    /* A Particular address */
  106.          stype = 0;
  107.          strcpy(srchstr, ++argv[i]);    /* get the key */
  108.          break;
  109.  
  110.         case 'c':    /* a Class of addresses */
  111.          stype = 1;
  112.          strcpy(srchstr, ++argv[i]);
  113.          break;
  114.  
  115.         default:
  116.          printf("\n??? Bad Switch");
  117.          goto finished;
  118.         }
  119.     }
  120.  
  121.  strcat(srchstr, "]");
  122.   
  123.  scanbuf[srchlen = strlen(srchstr)] = '\0';    /* Scan this much */
  124.  
  125.  /* Fill the scan buffer initially */
  126.  done = 0;
  127.  for (i = 0; i < srchlen; ++i) {
  128.     scanbuf[i] = getadrs();
  129.     if (done) goto finished;
  130.  }
  131.  
  132.  /* Make labels out of the appropriate addresses */
  133.  labcnt = nlabls = 0;
  134.  while ((chr = getadc()) != 0xff) {
  135.     switch (chr) {
  136.       case '[':        
  137.         if (stype) { /* Class Catagory check */
  138.             while ((chr = getadc()) != 0xff) {
  139.                 switch (chr) {
  140.                  case ' ':
  141.                     if (keymatch()) {
  142.                         putlabel();
  143.                         goto nxtadr;
  144.                     }
  145.                     break;
  146.                  case '[': /* Skip phone number */
  147.                     nxtline();
  148.                     goto nxtgreet;
  149.                 }
  150.             }
  151.             goto finished;  /* EOF encountered */
  152.         }    
  153.         else { /* Find individual address */
  154.             if (keymatch()) {
  155.                 putlabel();
  156.             }
  157.             else {
  158.                 nxtline();
  159.                 goto nxtgreet;
  160.             }
  161.         }
  162.         break;
  163.     }
  164. nxtadr:    continue;
  165. nxtgreet: /* scan to next '[' (the greeting) */
  166.     while ((chr = getadc()) != 0xff) {
  167.         if (chr == '[') break; }
  168. }
  169.  
  170. finished:
  171.     fflush(&labl_file);
  172.     close(labl_fd);
  173.     printf("All Done, %u Labels Generated", nlabls);
  174.     exit();
  175. }
  176.  
  177. char getadrs()
  178. {
  179.  /* Get a character from the address file,
  180.      checking for the end of file */
  181.  
  182. if ((ichr = getc(&adrs_file)) == ERROR) done = TRUE;
  183. return (ichr);
  184. }
  185.  
  186. char getadc()    
  187. {
  188.  char
  189.     chr;
  190.  
  191.     chr = scanbuf[0];
  192.      /* Shift the scan buffer down a character */
  193.     for (i = 0; i < srchlen; ++i) {
  194.         scanbuf[i] = scanbuf[i+1]; }
  195.     scanbuf[srchlen-1] = getadrs();
  196.     return (chr);
  197. }
  198.  
  199. char keymatch()
  200. {
  201.  /* Return TRUE if srchstr == scanbuf */
  202.  return (strcmp(srchstr, scanbuf) == 0);
  203. }
  204.  
  205. nxtline()
  206. {
  207.  /* Move to the next line */
  208.  while ((chr = getadc()) != 0xff) {
  209.     if (chr == LF) return; }
  210. }
  211.  
  212. putlabel()
  213. {
  214.  /* Output a label to the labl_file */
  215.  
  216.  nxtline();
  217.  
  218.  /* Check for a form feed every NFORMS labels */
  219.  if (labcnt++ >= NFORMS) {
  220.     labcnt = 0;
  221.     putlab(FF);
  222.  }
  223.  
  224.  /* Output the label, counting lines */
  225.  printf("\n Label %u:\n\n", ++nlabls);
  226.  lablin = 0;
  227.  while ((chr = getadc()) != 0xff) {
  228.     if (chr == '[') goto labdone;
  229.     if (chr == LF) {
  230.         if (++lablin > FORMLINES) {
  231.             printf("Label %u has too many lines", nlabls);
  232.             goto labdone;
  233.         }
  234.     }
  235.     putlab(chr);
  236.     putchar(chr);    /* console echo */
  237.  }
  238. labdone:
  239.     /* Space to the next label */
  240.     for (; lablin < (FORMLINES + FORMSPACE); ++lablin) {
  241.         putlab(CR);
  242.         putlab(LF);
  243.         if (!stype) break;    /* only one label */
  244.     }
  245. }
  246.  
  247. putlab(chr)
  248. char chr;
  249. {
  250.     putc(chr, &labl_file);
  251.     }
  252.